#include "../mqserver/mq_message.hpp"
#include "gtest/gtest.h"
#include <iostream>

mymq::QueueManager::Ptr qmp;

class QueueMsgTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        qmp = std::make_shared<mymq::QueueManager>("./data/message/");
        qmp->InitQueueMessage("queue1");
    }
    virtual void TearDown() override
    {
        qmp->Clear();
        std::cout << "最后的清理！！\n";
    }
};
//插入测试
TEST(queuemsg_test,insert_test)
{
    mymq::BasicProperties bp;
    bp.set_delivery(mymq::DeliveryMode::UNDURABLE);
    bp.set_id(mymq::GetUUIDHelper::GetUUID());
    bp.set_routing_key("music.pop.#");
    //qmp->Insert("queue1",&bp,"Hello world-1",true);
    qmp->Insert("queue1",nullptr,"Hello world-2",true);
    qmp->Insert("queue1",nullptr,"Hello world-3",true);
    qmp->Insert("queue1",nullptr,"Hello world-4",true);
    qmp->Insert("queue1",nullptr,"Hello world-5",true);
    //添加了五条消息，4条持久保存  durable_count=4,total_count=5
    ASSERT_EQ(qmp->durable_count("queue1"),4);
    ASSERT_EQ(qmp->total_count("queue1"),4);
}
//恢复测试
// TEST(queuemsg_test,recovery_test)
// {
//     //当前恢复之后只剩4条有效数据
//     ASSERT_EQ(qmp->getable_count("queue1"),4);
//     ASSERT_EQ(qmp->durable_count("queue1"),4);
//     ASSERT_EQ(qmp->total_count("queue1"),4);
// }

//获取队头数据，推送消息
TEST(queuemsg_test,front_test)
{
    mymq::MessagePtr msg = qmp->Front("queue1");
    ASSERT_EQ(qmp->getable_count("queue1"),3);
    ASSERT_EQ(qmp->waitack_count("queue1"),1);
    qmp->Ack("queue1",msg->payload().properties().id());
    ASSERT_EQ(qmp->waitack_count("queue1"),0);
    ASSERT_EQ(qmp->durable_count("queue1"),3);
    ASSERT_EQ(qmp->total_count("queue1"),3);
    qmp->Front("queue1");
    ASSERT_EQ(qmp->getable_count("queue1"),2);
    ASSERT_EQ(qmp->waitack_count("queue1"),1);
    qmp->Front("queue1");
    ASSERT_EQ(qmp->getable_count("queue1"),1);
    ASSERT_EQ(qmp->waitack_count("queue1"),2);
}

int main(int argc,char* argv[])
{
    testing::AddGlobalTestEnvironment(new QueueMsgTest);
    testing::InitGoogleTest();
    return RUN_ALL_TESTS();
}